package com.ptadmin.report.mws.sample;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.amazonaws.mws.MarketplaceWebService;
import com.amazonaws.mws.MarketplaceWebServiceClient;
import com.amazonaws.mws.MarketplaceWebServiceConfig;
import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.GetReportRequest;
import com.amazonaws.mws.model.GetReportResponse;
import com.amazonaws.mws.model.ResponseMetadata;

public class GetReport {

	/**
	 * Just add a few required parameters, and try the service Get Report
	 * functionality
	 *
	 * @param args
	 *            unused
	 */
	public static void main(String... args) {

		/************************************************************************
		 * Access Key ID and Secret Access Key ID, obtained from: http://aws.amazon.com
		 ***********************************************************************/
		final String accessKeyId = "AKIAI2JEZAYVZZZ3GHGA";
		final String secretAccessKey = "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";

		final String appName = "ptadmin";
		final String appVersion = "1.0.0";

		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();

		/************************************************************************
		 * Uncomment to set the appropriate MWS endpoint.
		 ************************************************************************/
		config.setServiceURL("https://mws.amazonservices.com/");

		/************************************************************************
		 * You can also try advanced configuration options. Available options are:
		 *
		 * - Signature Version - Proxy Host and Proxy Port - User Agent String to be
		 * sent to Marketplace Web Service
		 *
		 ***********************************************************************/

		/************************************************************************
		 * Instantiate Http Client Implementation of Marketplace Web Service
		 ***********************************************************************/

		MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName,
				appVersion, config);

		/************************************************************************
		 * Setup request parameters and uncomment invoke to try out sample for Get
		 * Report
		 ***********************************************************************/

		/************************************************************************
		 * Marketplace and Merchant IDs are required parameters for all Marketplace Web
		 * Service calls.
		 ***********************************************************************/
		final String merchantId = "A2N95VZZXMVRLW";
		final String sellerDevAuthToken = "amzn.mws.52b2d181-1d65-65a8-04f7-e12ce26cfad5";

		GetReportRequest request = new GetReportRequest();
		request.setMerchant(merchantId);
		// request.setMWSAuthToken(sellerDevAuthToken);

		request.setReportId("12320914324017859");

		// Note that depending on the type of report being downloaded, a report can reach 
        // sizes greater than 1GB. For this reason we recommend that you _always_ program to
        // MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
        // the in-memory size limit and have to re-work your solution.
        //
        OutputStream report;
		try {
			report = new FileOutputStream( "report.xml" );
			request.setReportOutputStream(report);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        invokeGetReport(service, request);
	}

	/**
	 * Get Report request sample The GetReport operation returns the contents of a
	 * report. Reports can potentially be very large (>100MB) which is why we only
	 * return one report at a time, and in a streaming fashion.
	 * 
	 * @param service
	 *            instance of MarketplaceWebService service
	 * @param request
	 *            Action to invoke
	 */
	public static void invokeGetReport(MarketplaceWebService service, GetReportRequest request) {
		try {

			GetReportResponse response = service.getReport(request);

			System.out.println("GetReport Action Response");
			System.out.println("=============================================================================");
			System.out.println();

			System.out.print("    GetReportResponse");
			System.out.println();
			System.out.print("    GetReportResult");
			System.out.println();
			System.out.print("            MD5Checksum");
			System.out.println();
			System.out.print("                " + response.getGetReportResult().getMD5Checksum());
			System.out.println();
			if (response.isSetResponseMetadata()) {
				System.out.print("        ResponseMetadata");
				System.out.println();
				ResponseMetadata responseMetadata = response.getResponseMetadata();
				if (responseMetadata.isSetRequestId()) {
					System.out.print("            RequestId");
					System.out.println();
					System.out.print("                " + responseMetadata.getRequestId());
					System.out.println();
				}
			}
			System.out.println();

			System.out.println("Report");
			System.out.println("=============================================================================");
			System.out.println();
			System.out.println(request.getReportOutputStream().toString());
			System.out.println();

			System.out.println(response.getResponseHeaderMetadata());
			System.out.println();

		} catch (MarketplaceWebServiceException ex) {

			System.out.println("Caught Exception: " + ex.getMessage());
			System.out.println("Response Status Code: " + ex.getStatusCode());
			System.out.println("Error Code: " + ex.getErrorCode());
			System.out.println("Error Type: " + ex.getErrorType());
			System.out.println("Request ID: " + ex.getRequestId());
			System.out.print("XML: " + ex.getXML());
			System.out.println("ResponseHeaderMetadata: " + ex.getResponseHeaderMetadata());
		}
	}

}
